package org.yoao.crm.business.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.yoao.crm.bean.Company;
import org.yoao.crm.business.service.ICompanyService;
import org.yoao.crm.dao.CompanyDAO;
import org.yoao.crm.struts.form.CompanyForm;

/**
 * @author yxwonline.com
 * @description 
 * @date 2010-7-31 下午02:15:45
 */
public class CompanyServiceImpl implements ICompanyService {
	private CompanyDAO companyDAO = new CompanyDAO();
	
	@Override
	public List view(int startRow, int endRow, Map map) throws Exception {
		return companyDAO.find(startRow, endRow, map);
	}
	
	@Override
	public List getCompanys() throws Exception {
		return companyDAO.findAll();
	}
	
	@Override
	public int getDataCount(Map map) throws Exception {
		return companyDAO.getDataCount(map);
	}
	
	@Override
	public CompanyForm add(CompanyForm form) throws Exception {
		Company company = new Company();
		BeanUtils.copyProperties(company, form);
		Company tmp = companyDAO.findCompanyByCompanyId(company.getCompanyId());
		if(tmp != null){
			form.setInfo("公司已存在,重复!");
		}else{
			int result = companyDAO.insert(company);
			if(result > -1){
				form.setId(result);
				form.setInfo("数据保存成功.");
			}else form.setInfo("数据保存失败.");
		}
		return form;
	}

	@Override
	public void delete(String id) throws Exception {
		String[] idArr = id.split(",");
		if(idArr != null && idArr.length > 0){
			for(int i=0;i<idArr.length;i++){
				companyDAO.delete(idArr[i]);
			}
		}
	}

	@Override
	public CompanyForm getCompany(String id) throws Exception {
		CompanyForm form = new CompanyForm();
		Company company = companyDAO.findById(id);
		if(company != null) BeanUtils.copyProperties(form, company);
		return form;
	}

	@Override
	public CompanyForm modify(CompanyForm form) throws Exception {
		Company company = new Company();
		BeanUtils.copyProperties(company, form);
		if(companyDAO.modify(company) > -1){
			form.setInfo("数据保存成功.");
		}else  form.setInfo("数据保存失败.");
		return form;
	}
}